package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    //根据用户名查询菜单列表
    @RequestMapping("/getMenuList")
    public Result getMenuList(String username){
        try {
            List<Menu> menuList = menuService.getMenuListByUsername(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return menuService.findPage(queryPageBean);
    }

    //根据用户名查询菜单列表
    @RequestMapping("/findParentMenuId")
    public Result findParentMenuId(){
        try {
            List<Menu> menuList = menuService.findParentMenuId();
            return new Result(true,MessageConstant.GET_PARENTMENULD_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_PARENTMENULD_FAIL);
        }
    }

    //根据菜单id查询菜单，回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findMenuById(id);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try{
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    //根据id删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }
    @RequestMapping("getTreeNodeByParentId")
    public Result getTreeNodeByParentId(@RequestParam Integer pid){
        List<Menu> childrenMenu = menuService.getTreeNodeByParentId(pid);
        if(childrenMenu != null && childrenMenu.size() > 0){
            return new Result(true,"OK",childrenMenu);
        }else {
            return new Result(false, "根据ID:[" + pid + "]查无子菜单", childrenMenu);
        }
    }


}
